// pages/signed/signed.js
const util = require('../../utils/util.js');
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    markers: [
      // {year: 2022, month: 3, day: 1, type: 'corner', mark: '', color: ''},
      // {year: 2022, month: 3, day: 2, type: 'schedule', mark: '', color: '', bgColor: ''},
      // {year: 2022, month: 3, day: 3, type: 'holiday', mark: '', color: '', bgColor: ''}
    ],
    yearMonth: null,  // 当前日历年月 yearMonth
    pickerDate: null,
    year: null,
    month: null,
    day: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '打卡'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '打卡'
    }
  },

  /**
   * 日历组件加载完成, 设置签到记录
   */
  handleCalendarLoad({detail}) {
    // console.log('calendar-load', detail)
    const yearMonth = detail.date.year +'-'+ (detail.date.month>=10? detail.date.month: '0'+ detail.date.month);
    this.setData({
      yearMonth: yearMonth,
      pickerDate: yearMonth +'-'+ (detail.date.day>=10? detail.date.day: '0'+ detail.date.day),
      year: detail.date.year,
      month: detail.date.month,
      day: detail.date.day
    })
    this.reqHasSignedData();
  },

  /**
   * 日期选择改变
   */
  handleCalendarDateChange({detail}) {
    // console.log(detail)
    let isLoad = false;
    const currDate = detail.date.year +'-'+ (detail.date.month>=10? detail.date.month: '0'+ detail.date.month);
    // 月份不相等, 重新获取数据
    if (this.data.yearMonth && this.data.yearMonth!=currDate) {
      isLoad = true;
    }
    
    this.setData({
      yearMonth: currDate,
      pickerDate: currDate +'-'+ (detail.date.day>=10? detail.date.day: '0'+ detail.date.day),
      year: detail.date.year,
      month: detail.date.month,
      day: detail.date.day
    })

    if (isLoad) {
      this.reqHasSignedData();
    }
  },

  /**
   * 请求当前月份已签到的数据
   */
  reqHasSignedData() {
    const param = {
      userId: wx.getStorageSync('userId'), year: this.data.year, month: this.data.month
    }
    request.GET('/sign/calDates', param).then(res => {
      if (res.data.resCode==200) {
        this.setData({
          markers: res.data.markersDate
        })
        // 重新设置标记模板
        this.calendar = this.selectComponent('#calendar');
        this.calendar.setMarkers(this.data.markers);
      }
    })
  },
  
  /**
   * 保存签到
   */
  clickSaveSigned() {
    
    const today = util.formatDate(new Date())
    if (this.data.pickerDate>today) {
      wx.showToast({
        title: '签到日期不能大于当天',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    wx.showToast({
      title: '签到中...',
      icon: 'loading',
      duration: 300000,
      mask: true
    })

    const param = {
      userId: wx.getStorageSync('userId'), dateStr: this.data.pickerDate, 
      year: this.data.year, 
      month: this.data.month, day: this.data.day
    }
    request.GET('/sign/saveWorkSign', param).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        wx.showToast({
          title: '签到成功',
          icon: 'success',
          duration: 1500
        })
        this.reqHasSignedData();
      } else if (res.data.resCode==201) {
        wx.showToast({
          title: '您已签到, 请勿重复操作',
          icon: 'none',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '签到异常',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

})